﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class SystemINI
    {

        [System.Runtime.InteropServices.DllImportAttribute("Kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [System.Runtime.InteropServices.DllImportAttribute("Kernel32.dll")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        public String DataSource;
        public String InitialCatalog;
        public String UserID;
        public String Password;
        public String DBConnectionString;
        public string type;


        public String OtherDataSource;
        public String OtherInitialCatalog;
        public String OtherUserID;
        public String OtherPassword;
        public String DBOtherConnectionString;
        public string type1;

        public string OKbarcode;
        public string Clearbarcode;
        public string Repairbarcode;

        public string PrintName1;
        public string PrintName2;

        public string LogPath;

        public string ProjectName;
        public string UserName;
        public string Pwd;
        public string IsAuto;




        public static SystemINI ReadINIFile()
        {
            string iniFilePath = System.Windows.Forms.Application.StartupPath + "\\system.ini";

            SystemINI sysini = new SystemINI();

            System.Text.StringBuilder sBuilder = new StringBuilder(255);


            //database
            GetPrivateProfileString("database", "DataSource", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.DataSource = sBuilder.ToString();

            GetPrivateProfileString("database", "InitialCatalog", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.InitialCatalog = sBuilder.ToString();

            GetPrivateProfileString("database", "UserID", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.UserID = sBuilder.ToString();

            GetPrivateProfileString("database", "Password", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.Password = sBuilder.ToString();

            GetPrivateProfileString("database", "type", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.type = sBuilder.ToString();


            //alwaysdatabase
            GetPrivateProfileString("otherDataBase", "otherDataSource", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.OtherDataSource = sBuilder.ToString();

            GetPrivateProfileString("otherDataBase", "otherInitialCatalog", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.OtherInitialCatalog = sBuilder.ToString();

            GetPrivateProfileString("otherDataBase", "otherUserID", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.OtherUserID = sBuilder.ToString();

            GetPrivateProfileString("otherDataBase", "otherPassword", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.OtherPassword = sBuilder.ToString();

            GetPrivateProfileString("otherDataBase", "type1", "IP NUll", sBuilder, 255, iniFilePath);
            sysini.type1 = sBuilder.ToString();


            if (sysini.type == "0")
            {
                sysini.DBConnectionString = "Data Source=" + sysini.DataSource +
                          ";Initial Catalog=" + sysini.InitialCatalog +
                          ";Persist Security Info=True;User ID=" + sysini.UserID +
                          ";Password=" + sysini.Password + "";
            }
            else
            {
                sysini.DBConnectionString = "Data Source=" + sysini.DataSource +
                          ";Initial Catalog=" + sysini.InitialCatalog +
                          ";Integrated Security=True";
            };



            if (sysini.type1 == "0")
            {
                sysini.DBOtherConnectionString = "Data Source=" + sysini.OtherDataSource +
                          ";Initial Catalog=" + sysini.OtherInitialCatalog +
                          ";Persist Security Info=True;User ID=" + sysini.OtherUserID +
                          ";Password=" +sysini.OtherPassword + "";

            }
            else
            {
                sysini.DBOtherConnectionString = "Data Source=" + sysini.OtherDataSource +
                           ";Initial Catalog=" + sysini.OtherInitialCatalog +
                            ";Integrated Security=True";
            }

            //PrintSet


            GetPrivateProfileString("BarcodeSet", "OKbarcode", "111111", sBuilder, 255, iniFilePath);
            sysini.OKbarcode = sBuilder.ToString();

            GetPrivateProfileString("BarcodeSet", "Clearbarcode", "222222", sBuilder, 255, iniFilePath);
            sysini.Clearbarcode = sBuilder.ToString();


            GetPrivateProfileString("BarcodeSet", "Repairbarcode", "333333", sBuilder, 255, iniFilePath);
            sysini.Repairbarcode = sBuilder.ToString();


            //PrintName
            GetPrivateProfileString("PrintSet", "AName", "NPI3D273D", sBuilder, 255, iniFilePath);
            sysini.PrintName1 = sBuilder.ToString();

            GetPrivateProfileString("PrintSet", "BName", "NPI3D273D", sBuilder, 255, iniFilePath);
            sysini.PrintName2 = sBuilder.ToString();


            GetPrivateProfileString("LogSet", "LogPath", @"D:\log", sBuilder, 255, iniFilePath);
            sysini.LogPath = sBuilder.ToString();


            GetPrivateProfileString("AutoSet", "ProjectName", "", sBuilder, 255, iniFilePath);
            sysini.ProjectName = sBuilder.ToString();
            GetPrivateProfileString("AutoSet", "UserName", "", sBuilder, 255, iniFilePath);
            sysini.UserName = sBuilder.ToString();
            GetPrivateProfileString("AutoSet", "Pwd", "", sBuilder, 255, iniFilePath);
            sysini.Pwd = sBuilder.ToString();
            GetPrivateProfileString("AutoSet", "IsAuto", "", sBuilder, 255, iniFilePath);
            sysini.IsAuto = sBuilder.ToString();

            return sysini;
        }


    }
}
